<template>
  <div>

    <el-form :model="dataParams" ref="queryForm" v-show="showSearch" label-width="68px"
             style="background-color: white;padding: 10px;margin-top: 10px;margin-left:10px; margin-right: 10px;">
      <el-row>

        <el-col :span="6">
          <el-form-item label="关键词" prop="keyWords">
            <el-input
              v-model="dataParams.keyWords"
              placeholder="请输入关键词,多个关键词用','连接"
              size="small"
              style="width: 300px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>


        <el-col :span="3">
          <el-form-item label="位置" prop="searchArea">
            <el-select v-model="dataParams.searchArea" size="small" style="width: 140px">
              <el-option v-for="dict in searchAreaOptions"
                         :key="dict.dictValue"
                         :label="dict.dictLabel"
                         :value="dict.dictValue"
              >

              </el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="9">
          <el-form-item label="时间">
            <el-radio-group size="small" v-model="timeType" @change="onChangeGroup">
              <el-radio-button v-for="(option, index) in options" :key="index"
                               :label="option.value">{{option.label}}
              </el-radio-button>
            </el-radio-group>

            <el-date-picker
              size="small"
              value-format="yyyy-MM-dd"
              style="margin-left: 10px;width: 240px"
              v-if="showSelectData"
              v-model="value1"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
            </el-date-picker>
          </el-form-item>
        </el-col>


        <el-col :span="6">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
            <!--            <el-button icon="el-icon-download" size="small" @click="downloadData" :loading="exportLoading"-->
            <!--                       v-show="total>0">下载到本地-->
            <!--            </el-button>-->
          </el-form-item>
        </el-col>
      </el-row>


    </el-form>


    <el-table v-loading="loading" :data="opinionList" style="min-height: 680px">

      <el-table-column label="发布日期" align="center" prop="publishTime" width="180">
        <template slot-scope="scope">
          <span>{{ globalFormatDateTime(scope.row.publishTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="标题" prop="title">
        <template slot-scope="scope">
          <el-link :href="scope.row.url" target="_blank" v-html="scope.row.title" class="single-line"></el-link>
        </template>

      </el-table-column>
      <el-table-column label="媒体名称" prop="captureWebsite" width="200"/>
      <el-table-column label="作者" prop="author" width="200"/>

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      layout="total, sizes,next"
      @pagination="getList"
    />


  </div>
</template>

<script>
  import {
    queryOpinionByDataServer,
    downloadCsvDataFromServeCenter
  } from "@/api/system/opinion";

  export default {
    name: "Opinion",
    components: {},
    data() {
      return {
        // 导出遮罩层
        exportLoading: false,
        searchAreaOptions: [],
        sensitiveInformation: null,
        showSelectData: false,
        value1: [],
        timeType: 'today',
        options: [
          {label: '今天', value: 'today'},
          {label: '昨天', value: 'yesterday'},
          {label: '近7天', value: 'week'},
          {label: '近30天', value: 'month'},
          {label: '自定义', value: 'define'},
        ],

        // 遮罩层
        loading: false,

        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 舆情表格数据
        opinionList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 15,
        },
        dataParams: {
          timeType: this.timeType,
          startDateTime: "",
          endDateTime: "",
          keyWords: null,
          searchArea: null,

        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          title: [
            {required: true, message: "标题不能为空", trigger: "blur"}
          ],
          content: [
            {required: true, message: "内容不能为空", trigger: "blur"}
          ],
        },

      };
    },
    created() {
      if (this.$route.query.timeType != null) {
        this.timeType = this.$route.query.timeType
      }
      if (this.$route.query.keyWords != null) {
        this.dataParams.keyWords = this.$route.query.keyWords
      }
      if (this.$route.query.beginPublishDay != null) {
        this.dataParams.startDateTime = this.$route.query.beginPublishDay
        let startDateTime = this.$route.query.beginPublishDay
        this.showSelectData = true;
        this.value1.push(startDateTime)
      }
      if (this.$route.query.endPublishDay != null) {
        this.dataParams.endDateTime = this.$route.query.endPublishDay
        let endDateTime = this.$route.query.endPublishDay
        this.showSelectData = true;
        this.value1.push(endDateTime)
      }

      this.getDicts("search_area_options").then(response => {
        this.searchAreaOptions = response.data;
      });

    },
    methods: {
      downloadData() {
        this.dataParams.timeType = this.timeType;
        if (this.value1 != null && this.value1.length !== 0) {
          this.dataParams.startDateTime = this.value1[0]
          this.dataParams.endDateTime = this.value1[1]
        }
        this.exportLoading = true;

        downloadCsvDataFromServeCenter(this.dataParams, this.queryParams).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
          this.msgSuccess(response.msg);
        })
      },


      onChangeGroup() {
        this.showSelectData = this.timeType === "define" ? true : false
        if (this.timeType !== "define") {

        }
      },
      /** 查询舆情列表 */
      getList() {
        this.loading = true;
        this.dataParams.timeType = this.timeType;
        if (this.value1 != null && this.value1.length !== 0) {
          this.dataParams.startDateTime = this.value1[0]
          this.dataParams.endDateTime = this.value1[1]
        }

        queryOpinionByDataServer(this.dataParams, this.queryParams).then(response => {
          this.opinionList = response.data.list;
          this.total = Number(response.data.totalCount);
          this.loading = false;
        });
      },


      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },


    }
  };
</script>

<style scoped lang="scss">

  .single-line {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis; /* 如果需要，可以使用省略号来表示被截断的文本 */
  }

  .article-title {
    font-size: 14px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    display: inline-block;
    width: 100%;
    color: #1e1e1e
  }

</style>
